package com.usaas.search.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usaas.common.core.domain.AjaxResult;
import com.usaas.search.dto.PlatUserFeedbackDto;
import com.usaas.search.mapper.PlatUserFeedbackMapper;
import com.usaas.search.pojo.PlatUserFeedbackPojo;
import com.usaas.search.service.IPlatUserFeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户反馈Service业务层处理
 *
 * @author Helios
 * @date 2024-08-14
 */
@Slf4j
@Service
public class PlatUserFeedbackServiceImpl extends ServiceImpl<PlatUserFeedbackMapper, PlatUserFeedbackPojo> implements IPlatUserFeedbackService {

    @Resource
    private PlatUserFeedbackMapper platUserFeedbackMapper;

    /**
     * 查询用户反馈
     *
     * @param id 用户反馈主键
     * @return 用户反馈
     */
    @Override
    public PlatUserFeedbackDto queryById(Long id) {
        PlatUserFeedbackPojo pojo = this.getById(id);
        if (pojo != null) {
            PlatUserFeedbackDto dto = BeanUtil.copyProperties(pojo, PlatUserFeedbackDto.class);
            List<PlatUserFeedbackDto> dtos = ListUtil.of(dto);
            convertList(dtos);
            return dtos.get(0);
        }
        return null;
    }

    /**
     * 查询用户反馈列表
     *
     * @param platUserFeedbackDto 用户反馈
     * @return 用户反馈
     */
    @Override
    public List<PlatUserFeedbackDto> queryList(PlatUserFeedbackDto platUserFeedbackDto) {
        LambdaQueryWrapper<PlatUserFeedbackPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = this.buildWrapper(queryWrapper, platUserFeedbackDto);
        if (queryWrapper == null) {
            return null;
        }
        List<PlatUserFeedbackPojo> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        List<PlatUserFeedbackDto> dtoList = BeanUtil.copyToList(list, PlatUserFeedbackDto.class);
        convertList(dtoList);
        return dtoList;
    }

    private void convertList(List<PlatUserFeedbackDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return;
        }
    }

    /**
     * 新增用户反馈
     *
     * @param platUserFeedbackDto 用户反馈
     * @return 结果
     */
    @Override
    @Transactional
    public boolean add(PlatUserFeedbackDto platUserFeedbackDto) {
        PlatUserFeedbackPojo platUserFeedbackPojo = BeanUtil.copyProperties(platUserFeedbackDto, PlatUserFeedbackPojo.class);
        return this.save(platUserFeedbackPojo);
    }

    /**
     * 修改用户反馈
     *
     * @param platUserFeedbackDto 用户反馈
     * @return 结果
     */
    @Override
    @Transactional
    public boolean update(PlatUserFeedbackDto platUserFeedbackDto) {
        PlatUserFeedbackPojo platUserFeedbackPojo = BeanUtil.copyProperties(platUserFeedbackDto, PlatUserFeedbackPojo.class);
        return this.updateById(platUserFeedbackPojo);
    }

    /**
     * 批量删除用户反馈
     *
     * @param ids 需要删除的用户反馈主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeBatch(List<String> ids) {
        List<Long> idsLong = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return this.removeBatchByIds(idsLong);
    }


    /**
     * 分页查询${subTable.functionName}信息
     *
     * @param platUserFeedbackDto
     * @return
     */
    @Override
    public AjaxResult queryPage(PlatUserFeedbackDto platUserFeedbackDto) {
        Page<PlatUserFeedbackPojo> page = new Page<>(platUserFeedbackDto.getPageNum(), platUserFeedbackDto.getPageSize());
        LambdaQueryWrapper<PlatUserFeedbackPojo> wrapper = new LambdaQueryWrapper<PlatUserFeedbackPojo>();
        //此处可做前置判断
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        wrapper = buildWrapper(wrapper, platUserFeedbackDto);
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        Page<PlatUserFeedbackPojo> pageResult = this.page(page, wrapper);
        Page<PlatUserFeedbackDto> finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatUserFeedbackPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatUserFeedbackDto> dtos = BeanUtil.copyToList(records, PlatUserFeedbackDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return AjaxResult.success(finalResult);
    }

    private LambdaQueryWrapper<PlatUserFeedbackPojo> buildWrapper(LambdaQueryWrapper<PlatUserFeedbackPojo> wrapper, PlatUserFeedbackDto param) {
        log.info("buildPageWrapper param :{}", param);
        if (wrapper == null) {
            return null;
        }
        Long id = param.getId();
        if (id != null) {
            wrapper.eq(PlatUserFeedbackPojo::getId, id);
        }
        Long userId = param.getUserId();
        if (userId != null) {
            wrapper.eq(PlatUserFeedbackPojo::getUserId, userId);
        }
        String content = param.getContent();
        if (StrUtil.isNotBlank(content)) {
            wrapper.eq(PlatUserFeedbackPojo::getContent, content);
        }
        Long replyerId = param.getReplyerId();
        if (replyerId != null) {
            wrapper.eq(PlatUserFeedbackPojo::getReplyerId, replyerId);
        }
        String replyerContent = param.getReplyerContent();
        if (StrUtil.isNotBlank(replyerContent)) {
            wrapper.eq(PlatUserFeedbackPojo::getReplyerContent, replyerContent);
        }
        String createBy = param.getCreateBy();
        if (StrUtil.isNotBlank(createBy)) {
            wrapper.eq(PlatUserFeedbackPojo::getCreateBy, createBy);
        }
        Date createTime = param.getCreateTime();
        if (createTime != null) {
            wrapper.eq(PlatUserFeedbackPojo::getCreateTime, createTime);
        }
        String updateBy = param.getUpdateBy();
        if (StrUtil.isNotBlank(updateBy)) {
            wrapper.eq(PlatUserFeedbackPojo::getUpdateBy, updateBy);
        }
        Date updateTime = param.getUpdateTime();
        if (updateTime != null) {
            wrapper.eq(PlatUserFeedbackPojo::getUpdateTime, updateTime);
        }
        String remark = param.getRemark();
        if (StrUtil.isNotBlank(remark)) {
            wrapper.eq(PlatUserFeedbackPojo::getRemark, remark);
        }
        Long delFlag = param.getDelFlag();
        if (delFlag != null) {
            wrapper.eq(PlatUserFeedbackPojo::getDelFlag, delFlag);
        }
        log.info("buildPageWrapper result :{}", wrapper);
        return wrapper;
    }
}
